package com.anbinh.doctor.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.ItemSpecial;
import com.anbinh.doctor.model.ResponseSpecial.Child;
import com.anbinh.doctor.model.ResponseSpecial.Item_;
import com.anbinh.doctor.model.ResponseSpecial.Item__;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;

public class SpecialAdapter extends ArrayAdapter<ItemSpecial> {

	private static final int TYPE_ITEM = 0;
	public static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

	private LayoutInflater mInflater;
	private Context context;
	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
	private List<ItemSpecial> mData = new ArrayList<ItemSpecial>();
	private UserLogin userLogin;
	private IRcallBak callBak;

	public SpecialAdapter(Context mContext, int resource,
			List<ItemSpecial> models) {
		super(mContext, resource, models);
		mInflater = LayoutInflater.from(mContext);
		context = mContext;
		userLogin = Utils.getAccountModel(mContext, Utils.KEY_USER);
	}

	public List<ItemSpecial> getmData() {
		return mData;
	}

	public void onCheck(String idgroup, boolean isCheck) {
		for (int i = 0; i < mData.size(); i++) {
			if (idgroup.equals(mData.get(i).getIdGroupName())) {
				mData.get(i).getItem().getItem().setChecked(isCheck);
				List<Child> mListchild = mData.get(i).getItem().getChildren();
				if (mListchild != null && mListchild.size() > 0) {
					for (int j = 0; j < mListchild.size(); j++) {
						mListchild.get(j).getItem().setChecked(isCheck);
					}
				}
			}
		}
		notifyDataSetChanged();
	}

	public void onCheckItem(int postion, boolean isCheck) {
		for (int i = 0; i < mData.size(); i++) {
			if (i == postion) {
				mData.get(i).getChild().getItem().setChecked(isCheck);
			}
		}
		notifyDataSetChanged();
	}

	public void addItem(final ItemSpecial item) {
		mData.add(item);
		notifyDataSetChanged();
	}

	public void removeItem(int position) {
		mData.remove(position);
		notifyDataSetChanged();
	}

	public void RemoveData() {
		mData.removeAll(mData);
		mSeparatorsSet.removeAll(mSeparatorsSet);
		notifyDataSetChanged();
	}

	public void addSeparatorItem(final ItemSpecial item) {
		mData.add(item);
		mSeparatorsSet.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public ItemSpecial getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		int type = getItemViewType(position);
		if (convertView == null) {
			holder = new ViewHolder();
			switch (type) {
			case TYPE_SEPARATOR:

				convertView = mInflater.inflate(R.layout.seprator_chuyen_khoa,
						null);
				holder.cb = (CheckBox) convertView.findViewById(R.id.rdGroup);

				break;

			case TYPE_ITEM:

				convertView = mInflater
						.inflate(R.layout.item_chuyen_khoa, null);
				holder.cb = (CheckBox) convertView.findViewById(R.id.rdGroup);

				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ItemSpecial item = getItem(position);
		if (type == TYPE_SEPARATOR) {
			Item_ itemSp = item.getItem().getItem();
			holder.cb.setText(itemSp.getLabel());
			holder.cb.setChecked(itemSp.getChecked());
		} else {
			Item__ itemSp = item.getChild().getItem();
			holder.cb.setText(itemSp.getLabel());
			holder.cb.setChecked(itemSp.getChecked());

		}

		return convertView;
	}

	public static class ViewHolder {
		public CheckBox cb;

	}

}